﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Skins;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList;
using DevExpress.Utils.Design;
using DevExpress.XtraSplashScreen;

namespace DevExpress.MailClient.Win
{
    public partial class ucFolderStructureTree : BaseControl
    {
        [Browsable(false)]
        public PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT CURRENT_SELECTED_ARCHIVE_DEPARTMENT
        {
            get;
            set;
        }

        private PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE _cURRENT_SELECTED_ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE;
        [Browsable(false)]
        public PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE CURRENT_SELECTED_ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE
        {
            get { return _cURRENT_SELECTED_ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE; }
            set
            {
                _cURRENT_SELECTED_ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE = value;
                CURRENT_SELECTED_ARCHIVE_DEPARTMENT = _cURRENT_SELECTED_ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENT;
            }
        }
        [Browsable(false)]
        public ApplicationStructureTypes CURRENT_SELECTED_ApplicationStructureType
        {
            get;
            set;
        }

        public FolderStructureViewModes CURRENT_SELECTED_VIEW_MODE
        {
            get;
            set;
        }
        public enum FolderStructureViewModes
        {
            MainForm = 0,
            FolderSelection,
            FileCopyingSelection
        }

        bool IS_FIRST_LOAD
        { get; set; }
        public ucFolderStructureTree()
        {
            InitializeComponent();
            IS_FIRST_LOAD = true;
            CURRENT_SELECTED_ARCHIVE_DEPARTMENT = new PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT();
            CURRENT_SELECTED_ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE = new PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE();
        }
        protected override void LookAndFeelStyleChanged()
        {
            base.LookAndFeelStyleChanged();
            Color controlColor = CommonSkins.GetSkin(DevExpress.LookAndFeel.UserLookAndFeel.Default).Colors.GetColor("Control");
            trvDepartmentFolderStructure.Appearance.Empty.BackColor = controlColor;
            trvDepartmentFolderStructure.Appearance.Row.BackColor = controlColor;
        }
        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            RaiseDataSourceChanged(e.Node);
        }
        void RaiseDataSourceChanged(TreeListNode node)
        {
            if (!IS_FIRST_LOAD)
            {

                string nodeCation = GetNodeCaption(node);
                string nodeValue = node.GetValue(colData).ToString();
                object nodeTag = node.Tag;


                CURRENT_SELECTED_ApplicationStructureType = (node.Level.Equals(0)) ? ApplicationStructureTypes.Department : ApplicationStructureTypes.Folder;// (ApplicationStructureTypes)Convert.ToInt32(node.GetValue(colType).ToString());

                switch (CURRENT_SELECTED_ApplicationStructureType)
                {
                    case ApplicationStructureTypes.Department:
                        CURRENT_SELECTED_ARCHIVE_DEPARTMENT = (PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT)nodeTag;
                        break;
                    case ApplicationStructureTypes.Folder:
                        CURRENT_SELECTED_ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE = (PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE)nodeTag;
                        break;
                }
                switch (CURRENT_SELECTED_VIEW_MODE)
                {
                    case FolderStructureViewModes.MainForm:
                        {
                            frmMain aPPLICATION_MAIN_FORM = (frmMain)this.ParentForm;
                            Modules.DepartmentsFolderStructure ucDepartmentsFolderStructure = (Modules.DepartmentsFolderStructure)aPPLICATION_MAIN_FORM.pcMain.Controls[0];
                            ucDepartmentsFolderStructure.LOAD_ARCHIVE_DEPARTMENT_FILES(CURRENT_SELECTED_ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE);
                        }
                        break;
                    case FolderStructureViewModes.FolderSelection:
                        break;
                    case FolderStructureViewModes.FileCopyingSelection:
                        {

                        }
                        break;
                }
            }
        }
        string GetNodeCaption(TreeListNode node)
        {
            string ret = string.Format("{0}", node.GetValue(colName));
            while (node.ParentNode != null)
            {
                node = node.ParentNode;
                ret = string.Format("{0} - {1}", node.GetValue(colName), ret);
            }
            return ret;
        }
        static string GetHtmlTextColor(bool focused)
        {
            if (focused)
                return ColorHelper.HtmlHighlightTextColor;
            return AllowControlTextColor ? ColorHelper.HtmlControlTextColor : ColorHelper.HtmlControlTextColor2;
        }
        static bool AllowControlTextColor
        {
            get
            {
                return DevExpress.LookAndFeel.UserLookAndFeel.Default.ActiveSkinName != "Glass Oceans";
            }
        }
        internal void RefreshTreeList()
        {
            trvDepartmentFolderStructure.LayoutChanged();
        }

        internal void UpdateTreeViewMessages()
        {
            //CreateMessagesList(treeList1.Nodes);
            //RefreshTreeList();
            //RaiseDataSourceChanged(treeList1.FocusedNode);
        }

        public void BUILD_ARCHIVE_DEPARTMENT_FOLDER_STRUCTURE()
        {
            try
            {
                trvDepartmentFolderStructure.Nodes.Clear();

                List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT> aRCHIVE_DEPARTMENTS = PRO_DOCS.BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_ARCHIVES_SECURITY.CURRENT_LOGGED_IN_USER_ALLOWED_ARCHIVE_DEPARTMENTS.Values.ToList();//. //PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENTs.ToList();
                for (int i = 0; i < aRCHIVE_DEPARTMENTS.Count; i++)
                {
                    BUILD_ARCHIVE_DEPARTMENT_FOLDER_STRUCTURE(aRCHIVE_DEPARTMENTS[i]);
                }
                IS_FIRST_LOAD = false;
            }
            catch (Exception ex)
            {
                string abc = ex.Message;
            }

        }

        private void BUILD_ARCHIVE_DEPARTMENT_FOLDER_STRUCTURE_FOLDERS_SUBFOLDERS(PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE, TreeListNode trParentNode)
        {

            List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE> aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURES = aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTUREs.ToList();

            for (int i = 0; i < aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURES.Count; i++)
            {
                PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE _aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE = aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURES[i];
                if (PRO_DOCS.BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_ARCHIVES_SECURITY.CURRENT_LOGGED_IN_USER_ALLOWED_FOLDERS.ContainsKey(_aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID))
                {
                    TreeListNode tlFolderNode = trvDepartmentFolderStructure.AppendNode(new object[] { _aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_NAME, ApplicationStructureTypes.Folder, FolderTypes.Folder, 10, _aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID }, trParentNode, _aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE);

                    BUILD_ARCHIVE_DEPARTMENT_FOLDER_STRUCTURE_FOLDERS_SUBFOLDERS(_aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE, tlFolderNode);
                }
            }
        }

        public void BUILD_ARCHIVE_DEPARTMENT_FOLDER_STRUCTURE(PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT aRCHIVE_DEPARTMENT)
        {

            TreeListNode tlnDepartmentNode = trvDepartmentFolderStructure.AppendNode(new object[] { aRCHIVE_DEPARTMENT.ARCHIVE_DEPARTMENT_NAME, ApplicationStructureTypes.Department, FolderTypes.Folder, 12, aRCHIVE_DEPARTMENT.ARCHIVE_DEPARTMENT_ID }, null, aRCHIVE_DEPARTMENT);

            {
                //Gets the 1st Level
                List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE> aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURES = aRCHIVE_DEPARTMENT.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTUREs.ToList();


                for (int i = 0; i < aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURES.Count; i++)
                {
                    PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE = aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURES[i];

                    if (PRO_DOCS.BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_ARCHIVES_SECURITY.CURRENT_LOGGED_IN_USER_ALLOWED_FOLDERS.ContainsKey(aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID))
                    {

                        if (aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.PARENT_ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID.Value.Equals(Guid.Empty))
                        {
                            TreeListNode tlFolderNode = trvDepartmentFolderStructure.AppendNode(new object[] { aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_NAME, ApplicationStructureTypes.Folder, FolderTypes.Folder, 10, aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID }, tlnDepartmentNode, aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE);

                            BUILD_ARCHIVE_DEPARTMENT_FOLDER_STRUCTURE_FOLDERS_SUBFOLDERS(aRCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE, tlFolderNode);

                        }
                    }
                }

            }
        }

        private void trvDepartmentFolderStructure_AfterCollapse(object sender, NodeEventArgs e)
        {
            TreeListNode current_selected_node = e.Node;
            string node_type = current_selected_node.GetValue(colType).ToString();
            if (node_type.Equals(ApplicationStructureTypes.Folder.ToString()))
            {

                current_selected_node.SetValue(colImageIndex, 10);
            }

        }

        private void trvDepartmentFolderStructure_AfterExpand(object sender, NodeEventArgs e)
        {
            TreeListNode current_selected_node = e.Node;
            string node_type = current_selected_node.GetValue(colType).ToString();
            if (node_type.Equals(ApplicationStructureTypes.Folder.ToString()))
            {
                current_selected_node.SetValue(colImageIndex, 11);
            }
        }


        private void ucFolderStructureTree_Load(object sender, EventArgs e)
        {


            //BUILD_ARCHIVE_DEPARTMENT_FOLDER_STRUCTURE();


        }
    }
}
